# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=nuitka
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.6.5
pkgrel=1
pkgdesc="Python to native compiler (mingw-w64)"
arch=('any')
license=('APACHE')
url="https://www.nuitka.net/"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=("http://nuitka.net/releases/Nuitka-${pkgver}.tar.gz")
noextract=(Nuitka-$pkgver.tar.gz)
sha256sums=('c38dce3eb2b98a703c7e587b3c847c3ea87c434b36783eb5e26c7e613da88d9b')

prepare() {
  [[ -d $srcdir/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzvf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || \
  tar -xzvf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir}
}

build() {
  rm -rf python{2,3}-build
  for builddir in python{2,3}-build; do
    cp -r Nuitka-${pkgver} ${builddir}
    pushd ${builddir}
    ${MINGW_PREFIX}/bin/${builddir%-build} setup.py build
    popd
  done
}

package_python2-nuitka() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")
  
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python2-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
  for filename in nuitka nuitka-run; do
    sed -e "s|...\python|python2|g" \
        -e "s|${filename}|${filename}2|g" \
        -i ${filename}.bat
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${filename}-script.py
    #mv "${pkgdir}${MINGW_PREFIX}"/bin/${filename} "${pkgdir}${MINGW_PREFIX}"/bin/${filename}.exe
    #mv "${pkgdir}${MINGW_PREFIX}"/bin/${filename}.bat "${pkgdir}${MINGW_PREFIX}"/bin/${filename}2.bat
  done
  popd > /dev/null
}

package_python3-nuitka() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  
  cd "${srcdir}/python3-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  mv "${pkgdir}${MINGW_PREFIX}"/bin/nuitka.bat "${pkgdir}${MINGW_PREFIX}"/bin/nuitka3.bat
  mv "${pkgdir}${MINGW_PREFIX}"/bin/nuitka-run.bat "${pkgdir}${MINGW_PREFIX}"/bin/nuitka3-run.bat
  pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
  for filename in nuitka3 nuitka3-run; do
    sed -e "s|...\python|python3|g" \
        -i ${filename}.bat
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${filename}-script.py
  done
  popd > /dev/null

}

package_mingw-w64-i686-python2-nuitka() {
  package_python2-nuitka
}

package_mingw-w64-i686-python3-nuitka() {
  package_python3-nuitka
}

package_mingw-w64-x86_64-python2-nuitka() {
  package_python2-nuitka
}

package_mingw-w64-x86_64-python3-nuitka() {
  package_python3-nuitka
}
